﻿package Cain
{
	import flash.display.DisplayObject;
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 自動捲軸 v1.1
	 * cain@
	 */
	public class Grip
	{
		private var _m:DisplayObject;
		private var _m_top:Number;
		private var _m_foot:Number;
		private var dx_y:Number;
		private var _mc:DisplayObject;
		private var _t_top:Number;
		private var _t_foot:Number;
		private var _d:Number;
		private var wait:Number;
		private var sp:Sprite;
		/**
		 * 自動捲軸
		 * @param	m 拉把
		 * @param	m_top 最高點
		 * @param	m_foot 最底點
		 * @param	mc 內容物件
		 * @param	t_top 最高點
		 * @param	t_foot 最低點
		 * @param	_wait 拖曳延緩時間
		 */
		public function Grip(m:DisplayObject,m_top:Number,m_foot:Number,mc:DisplayObject,t_top:Number,t_foot:Number,_wait:Number = 1):void 
		{
			Sprite(m).buttonMode = true;
			_m = m;
			_m_top = m_top;
			_m_foot = m_foot;
			_mc = mc
			_t_top = t_top;
			_t_foot = t_foot;
			wait = _wait;
			_m.addEventListener(MouseEvent.MOUSE_DOWN, _m_mousedown);
			_m.stage.addEventListener(MouseEvent.MOUSE_UP, _m_mouseup);
			_d = (t_foot - t_top) / (m_foot - m_top);
			_mc.y = t_top - (_m_top - _m.y) * _d;
			_mc.addEventListener(MouseEvent.MOUSE_WHEEL, mouse_wheel);
			add_sp();
		}
		/**
		 * 內容最底位置
		 */
		public function set foot(t_foot:Number):void 
		{
			_t_foot = t_foot;
			_d = (_t_foot - _t_top) / (_m_foot - _m_top);
		}
		/**
		 * 內容物件移動
		 * @param	_y 位置座標
		 */
		public function goto(_y:Number):void 
		{
			Tweener.addTween(_mc, { y:_y, time:wait } );
			Tweener.addTween(_m,{y:(_y-_t_top)/_d+_m_top,time:wait});
		}
		private function add_sp():void
		{
			if (_mc is Sprite) 
			{
				sp = new Sprite();
				sp.graphics.beginFill(0, 0);
				var rc:Rectangle = _mc.getBounds(_mc);
				sp.graphics.drawRect(rc.x, rc.y, rc.width, rc.height);
				sp.graphics.endFill();
				Sprite(_mc).addChildAt(sp, 0);
			}
		}
		
		private function mouse_wheel(e:MouseEvent):void 
		{
			var wh:int = e.delta;
			var _mc_y:Number = _mc.y + wh * 30;
			if (_mc_y>_t_top) 
			{
				_mc_y = _t_top;
			}else if(_mc_y<_t_foot) 
			{
				
				_mc_y = _t_foot;
			}
			Tweener.addTween(_mc, { y:_mc_y, time:1 } );
			var _y:Number =  _m_top - (_t_top - _mc_y) / _d ;
			Tweener.addTween(_m,{y:_y,time:wait});
		}
		
		private function _m_mousedown(e:MouseEvent):void 
		{
			dx_y = _m.parent.mouseY - _m.y;
			_m.addEventListener(Event.ENTER_FRAME, _m_play);
		}
		
		private function _m_play(e:Event):void 
		{
			_m.y = _m.parent.mouseY - dx_y;
			if (_m.y < _m_top)
			{
				_m.y = _m_top;
			}else if (_m.y > _m_foot)
			{
				_m.y = _m_foot;
			}
			Tweener.addTween(_mc,{y:_t_top-(_m_top-_m.y)*_d,time:wait});
		}
		
		private function _m_mouseup(e:MouseEvent):void 
		{
			_m.removeEventListener(Event.ENTER_FRAME, _m_play);
		}
	}
	
}